// pages/information/information.js
var util = require('../../utils/util');
var App = getApp();
var util_md5 = require('../../utils/md5.js');
var util_sha1 = require('../../utils/sha1.js');
var API_Key = "chaotingjianshen0012";

function jsonSort(jsonObj) {
    let arr = [];
    for (var key in jsonObj) {
        arr.push(key)
    }
    arr.sort();
    let str = '';
    for (var i in arr) {
        str += arr[i] + "=" + jsonObj[arr[i]] + "&"
    }
    return str.substr(0, str.length - 1)
}

function coding(data) {
    var detail = util_sha1.sha1(jsonSort(data))
    var datas_detail = util_md5.hexMD5(detail)
    var str = datas_detail;
    str = str.toUpperCase();
    return str;
}
Page({

    /**
     * 页面的初始数据
     */
    data: {
        date: '',//传入时间
        date_month: '',//显示时间
        store_name: ['全部', '普通会员','私教会员'],
        genj:'',//潜客，会员
        name_num:0,
        vip_list:[],//会员数组
        caller_list:[],//潜客数组
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var This = this;
        var time = util.formatTime(new Date());
        var date_value = time.slice(0, 4) + "-" + time.slice(5, 7)
        var date_value_month = time.slice(0, 4) + "-" + time.slice(5, 7)
        // console.log(date_value)
        This.setData({
            date: date_value,
            date_month: date_value_month,
            genj:This.data.store_name[0],

        })
        console.log(This.data.date_month)
        this.loading();
    },
    bindDateChange: function (e) {
        var This = this;
        This.setData({
            information_list: []
        })
        var date_month = e.detail.value
        var year = date_month.slice(0, 4)
        var month = date_month.slice(5, 7)
        var date_time = year + '-' + month
        // console.log(e)
        // console.log(year)
        // console.log(month)
        // console.log(date_time)
        This.setData({
            date_month: date_time,
            date: e.detail.value 
        })
        console.log(This.data.date_month)
        this.loading();
    },
    bindPickerChange:function(e){
        var This = this;
        console.log(e)
        This.setData({
            genj: This.data.store_name[e.detail.value],
            name_num: e.detail.value
        })
        This.loading();
    },
    loading:function(){
        var This = this;
        var datas = {
            store_id: wx.getStorageSync('store_id'),
            openid: wx.getStorageSync('openid'),
            phone: wx.getStorageSync('phone'),
            date_str: This.data.date_month,
            type: parseInt(This.data.name_num),
            token: API_Key,
        }
        var str = coding(datas)
        App._get('coach/Callerdetail/get_coach_describe', {
            signature: str,
            store_id: wx.getStorageSync('store_id'),
            openid: wx.getStorageSync('openid'),
            phone: wx.getStorageSync('phone'),
            date_str:This.data.date_month,
            type: parseInt(This.data.name_num)
        }, function (res) {
            console.log(res)
            This.setData({
                vip_list: res.list,
                // caller_list:res.caller
            })
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})